package dth.bean;

import java.sql.Date;

import javax.faces.bean.ManagedBean;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.AnnotationConfiguration;

import dth.dao.FeedBackDAO;
import dth.dao.FeedBackDAO;
import dth.dao.FeedBackDAO;
import dth.entity.FeedBack;
import dth.entity.FeedBack;
import dth.entity.FeedBack;
import dth.utils.DthUtil;
import antlr.collections.List;

@ManagedBean(name = "feedbackBean")
public class FeedBackBean {
	private int id = 0;// use to insert delete
	private String feedbackTitle;
	private String feedbackContent;
	private int fkUserId;
	private Date dateInsert;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getFeedbackTitle() {
		return feedbackTitle;
	}
	public void setFeedbackTitle(String feedbackTitle) {
		this.feedbackTitle = feedbackTitle;
	}
	public String getFeedbackContent() {
		return feedbackContent;
	}
	public void setFeedbackContent(String feedbackContent) {
		this.feedbackContent = feedbackContent;
	}
	public int getFkUserId() {
		return fkUserId;
	}
	public void setFkUserId(int fkUserId) {
		this.fkUserId = fkUserId;
	}
	public Date getDateInsert() {
		return dateInsert;
	}
	public void setDateInsert(Date dateInsert) {
		this.dateInsert = dateInsert;
	}
	
	public FeedBackBean() {
		System.out.println("---request url:"+DthUtil.getRequestURL());
		if(DthUtil.getRequestURL().contains("feedback.xhtml")){
			System.out.println("--vao day role:"+LoginBean.roleCode);
			if(LoginBean.roleCode!=1)
				DthUtil.navigateTo("welcome.xhtml");
		}
		
		System.out.println("--feedback bean");
		Object objectAction = DthUtil.getRequest().getParameter("action");
		System.out.println("----------action:" + objectAction);
		if (objectAction != null) {
			if (objectAction.equals("delete")) {
				Object objectId = DthUtil.getRequest().getParameter("feedback_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id delete:" + id);
			} else if (objectAction.equals("update")) {
				Object objectId = DthUtil.getRequest().getParameter("feedback_id");
				if (objectId != null)
					id = Integer.parseInt(objectId.toString());
				System.out.println("---id edit:" + id);
				for (FeedBack feedback : getListFeedBack()) {
					if (feedback.getFeedbackId() == id) {
						this.feedbackTitle = feedback.getFeedbackTitle();
						this.feedbackContent = feedback.getFeedbackContent();
						this.dateInsert = feedback.getDateInsert();
						this.fkUserId = feedback.getFkUserId();
					}
				}
				System.out.println("-----title:" + feedbackTitle);
			}
		}
	}

	public java.util.List<FeedBack> getListFeedBack() {
		FeedBackDAO feedbackDAO = new FeedBackDAO();
		return feedbackDAO.getListFeedBack();
	}

	public void delete() {
		try {
			FeedBackDAO feedbackDAO = new FeedBackDAO();
			int count = feedbackDAO.delete(id);
			if(count>0)
				DthUtil.showMessage("Thông báo", "Đã xóa thành công");
			else
				DthUtil.showMessage("Thông báo", "Đã xóa thất bại");
		}
		catch(Exception ex){
			DthUtil.showMessage("Thông báo", "Có lỗi khi xóa");
		}
	}
	public void insert()
	{
		System.out.println("---id="+id);
		try{
			FeedBackDAO feedbackDAO=new FeedBackDAO();
			FeedBack feedback1=new FeedBack();
			feedback1.setFeedbackId(id);
			feedback1.setFeedbackTitle(feedbackTitle);
			feedback1.setFeedbackContent(feedbackContent);
			feedback1.setFkUserId(1);//set cung
			java.util.Date date=new java.util.Date();//lay ngay hien tai
			feedback1.setDateInsert(new Date(date.getTime()));
			int count=0;
			if(id==0)
				count=feedbackDAO.insert(feedback1);
			else
				count=feedbackDAO.update(feedback1);
			if(count>0)
				DthUtil.showMessage("Thông báo", "Đã insert thành công");
			else
				DthUtil.showMessage("Thông báo", "Đã insert thất bại");
		}
		catch(Exception ex){
			System.out.println("---------error:"+ex.getMessage());
			DthUtil.showMessage("Thông báo", "Đã insert thất bại");
		}
	}
	public void send()
	{
		System.out.println("---id="+LoginBean.id);
		try{
			FeedBackDAO feedbackDAO=new FeedBackDAO();
			FeedBack feedback1=new FeedBack();
			feedback1.setFeedbackId(id);
			feedback1.setFeedbackTitle(feedbackTitle);
			feedback1.setFeedbackContent(feedbackContent);
			feedback1.setFkUserId(LoginBean.id);//set cung
			java.util.Date date=new java.util.Date();//lay ngay hien tai
			feedback1.setDateInsert(new Date(date.getTime()));
			int count=0;
			if(id==0)
				count=feedbackDAO.insert(feedback1);
			else
				count=feedbackDAO.update(feedback1);
			if(count>0)
				DthUtil.showMessage("Thông báo", "Gửi thành công");
			else
				DthUtil.showMessage("Thông báo", "Không thể gửi, vui lòng thử lại.");
		}
		catch(Exception ex){
			System.out.println("---------error:"+ex.getMessage());
			DthUtil.showMessage("Thông báo", "Đã gửi thất bại");
		}
	}
}
